<template>
	<view style="padding: 20rpx;">
		<u--form labelPosition="left" :model="userInfo" label-width="60">
			<u-form-item label="姓名" prop="userInfo.name" borderBottom ref="item1">
				<u--input v-model="userInfo.userName"></u--input>
			</u-form-item>
			<u-form-item label="账号" prop="userInfo.userCode" borderBottom ref="item1">
				<u--input v-model="userInfo.userCode" disabled></u--input>
			</u-form-item>
			<u-form-item label="手机号" prop="userInfo.userMobile" borderBottom ref="item1">
				<u--input v-model="userInfo.userMobile"></u--input>
			</u-form-item>
			<u-form-item label="角色" prop="userInfo.roleName" borderBottom ref="item1">
				<u--input v-model="userInfo.roleName" disabled></u--input>
			</u-form-item>
			<u-form-item label="导师" prop="userInfo.tchName" borderBottom ref="item1" v-if="userInfo.roleId === 2">
				<u--input v-model="userInfo.tchName" disabled></u--input>
			</u-form-item>
			<u-form-item label="头像" prop="userInfo.userAvatar" borderBottom ref="item1">
				<u-upload @afterRead="afterRead" name="1" multiple :fileList="fileList1" style="margin-top: 20rpx;"
					@delete="handleDelete"></u-upload>
			</u-form-item>
			<u-form-item>
				<u-button type="primary" @click="updateProfile">提交</u-button>
			</u-form-item>
		</u--form>
	</view>
</template>

<script>
	import {
		updateProfileService
	} from '../../api/user';
	export default {
		data() {
			return {
				userInfo: uni.getStorageSync('user-profile'),
				fileList1: []
			};
		},
		onLoad() {
			this.getUserInfo()
			this.fileList1.push({
				url: 'http://localhost:8081' + this.userInfo.userAvatar
			})
		},
		methods: {
			async getUserInfo() {
				this.userInfo = await profileService()
				uni.setStorageSync("user-profile", this.userInfo)
			},
			afterRead(e) {
				console.log(e.file[0].url)
				uni.uploadFile({
					url: '/api/upload/img',
					name: 'file',
					filePath: e.file[0].url,
					header: {
						'Authorization': uni.getStorageSync("user-token")
					},
					success: (res) => {
						console.log(JSON.parse(res.data).data)
						this.userInfo.userAvatar = JSON.parse(res.data).data
						let url = {
							url: "http://localhost:8081" + JSON.parse(res.data).data
						}
						this.fileList1.push(url);
						console.log(this.fileList1)
					}
				})
			},
			handleDelete(event) {
				this.fileList1.pop();
			},
			async updateProfile() {
				await updateProfileService(this.userInfo)
				uni.showToast({
					icon: 'success',
					title: '修改成功'
				})
			}
		}
	}
</script>

<style lang="scss">

</style>